<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app">
      <div>
        <span>姓名:</span>
        <input v-model="newName" type="text" />
      </div>
      <div>
        <span>年龄:</span>
        <input v-model="newAge" type="number" />
      </div>
      <div>
        <span>性别:</span>
        <select v-model="newSex">
          <option value="男">男</option>
          <option value="女">女</option>
        </select>
      </div>
      <div>
        <!-- <button @click="isEditMode ? handleEditClick : handleAddClick">
          <span v-if="!isEditMode" @click="handleAddClick">添加</span>
          <span v-else @click="handleEditClick">修改</span>
        </button> -->
        <button>
          <span @click="handleClick()">添加/修改</span>
        </button>
        <!-- <button @click="toggleMode">切换模式</button> -->
      </div>
      <div>
        <table border="1" cellpadding="10" cellspacing="0">
          <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
          </tr>
          <tr v-for="(item,index) in userInfo" :key="item.id">
            <td>{{index+1}}</td>
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
            <td>{{item.sex}}</td>
            <td>
              <button @click="del(item.id)">删除</button>
              <button @click="edit(item.id)">编辑</button>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <script src="../vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        selectedId: "",
        data: {
          isEditMode: false,
          newName: "",
          newAge: "",
          newSex: "",
          userInfo: [
            {
              id: "001",
              name: "tom",
              age: "18",
              sex: "男",
            },
            {
              id: "002",
              name: "jike",
              age: "18",
              sex: "男",
            },
          ],
        },
        methods: {
          del(id) {
            this.userInfo = this.userInfo.filter((item) => item.id !== id);
            this.isEditMode = false;
          },
          // toggleMode() {
          //   this.isEditMode = !this.isEditMode;
          // },
          handleClick() {
            if (this.isEditMode) {
              this.userInfo.splice(
                this.userInfo.findIndex((item) => item.id === this.selectedId),
                1,
                {
                  id: this.selectedId,
                  name: this.newName,
                  age: this.newAge,
                  sex: this.newSex,
                }
              );
              this.isEditMode = !this.isEditMode;
            } else {
              if (this.newName.trim() === "") return alert("请输入姓名");
              if (this.newAge.trim() === "") return alert("请输入年龄");
              if (this.newSex.trim() === "") return alert("请选择性别");
              this.userInfo.push({
                id: +new Date(),
                name: this.newName,
                age: this.newAge,
                sex: this.newSex,
              });
              this.newName = "";
              this.newAge = "";
              this.newSex = "";
            }
          },
          edit(id) {
            this.newName = this.userInfo.find((item) => item.id === id).name;
            this.newAge = this.userInfo.find((item) => item.id === id).age;
            this.newSex = this.userInfo.find((item) => item.id === id).sex;
            this.selectedId = id;
            this.isEditMode = !this.isEditMode;
          },
        },
      });
    </script>
  </body>
</html>
